package com.lww.smartbathroom.control;

import com.lww.smartbathroom.model.Result;
import com.lww.smartbathroom.model.UserRecord;
import com.lww.smartbathroom.param.RecordParam;
import com.lww.smartbathroom.service.UserRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName UserRecordController.java
 * @Description TODO
 * @createTime 2021年01月24日 18:06:00
 */
@RestController
@RequestMapping("/userRecord")
public class UserRecordController {

    public static final Result<Object> RESULT = new Result<>();
    public static final Result<Object> OBJECT_RESULT = new Result<>();
    @Autowired
    private UserRecordService userRecordService;

    @GetMapping("/get")
    public Result get(@RequestParam("id")String id){
        Result result = new Result();
        result.setSuccess(true);
        result.setData(userRecordService.get(id));
        return result;
    }

    @PostMapping("/add")
    public UserRecord add(@RequestBody UserRecord userRecord) {

        if (userRecord.getAmount()==null){
            userRecord.setAmount(0.0);
        }
        userRecord.setStartTime(userRecord.getStartTime().minusHours(8));
        userRecord.setEndTime(userRecord.getEndTime().minusHours(8));
        return userRecordService.add(userRecord);
    }

    @PostMapping("/list")
    public Result list(@RequestBody RecordParam equipmentQueryParam) {

        Result result = new Result();
        if (equipmentQueryParam == null) {
            result.setMessage("param is null");

            result.setSuccess(false);
            return result;
        }

        Page<UserRecord> equipmentPage = userRecordService.list(equipmentQueryParam);
        result.setSuccess(true);
        result.setData(equipmentPage);
        return result;

    }
    @PostMapping("/modifiy")
    public boolean modifiy(@RequestBody UserRecord userRecord){
        return userRecordService.modify(userRecord);
    }
    @RequestMapping("/del")
    public boolean del(String id){
        return userRecordService.delete(id);
    }

    @GetMapping("/listAll")
    public Result listAll(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit,@RequestParam(value = "status",required = false)String status) {

        RecordParam equipmentQueryParam = new RecordParam();

        equipmentQueryParam.setPageSize(limit);
        equipmentQueryParam.setPageNum(page);
        if (StringUtils.hasText(status)&&!"ALL".equals(status)){
            equipmentQueryParam.setStatus(status);
        }
        Result result = new Result();
        if (equipmentQueryParam == null) {
            result.setMessage("param is null");

            result.setSuccess(false);
            return result;
        }
        Page<UserRecord> equipmentPage = userRecordService.list(equipmentQueryParam);
        result.setSuccess(true);
        result.setData(equipmentPage);

        return result;

    }

}
